//
//  YDOutOptionsCell.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDOutOptionsCell.h"
#import "YDOutOptionsM.h"


@interface YDOutOptionsCell ()


/**
 灰色顶
 */
@property (nonatomic, strong)UIView *topview;

/**
 股票名称
 */
@property (nonatomic, strong)UILabel *stockName;

/**
 股票代码
 */
@property (nonatomic, strong)UILabel *stockCode;

/**
 圆点
 */
@property (nonatomic, strong)UIView *yuanPoint;
/**
 自主行权
 */

@property (nonatomic, strong)UIImageView *jiantou;

@property (nonatomic, strong)UILabel *rights;

@property (nonatomic, strong)UILabel *line1;

/**
 中间详情
 */
@property (nonatomic, strong)UIView *middleView;

@property (nonatomic, strong)UILabel *line2;
/**
 底部详情
 */
@property (nonatomic, strong)UIView *bottomView;


@property (nonatomic, assign)NSInteger leftGap;

@property (nonatomic, strong)CAShapeLayer *lineLayer;

@end

@implementation YDOutOptionsCell


-(void)setModel:(YDOutOptionsM *)model
{
    _model = model;
   
    
    self.bottomView.hidden = !model.isSelected;
    self.line2.hidden = !model.isSelected;
    self.jiantou.image = [UIImage imageNamed:model.isSelected ?  @"qiquan_jiesuan_shou" : @"qiquan_jiesuan_kai"];
    
    
    
    self.stockName.text = model.stockName;
    self.stockCode.text = model.stockCode;
    if ([model.state isEqualToString:@"FAILURE"]) {
        //申购失败
        self.rights.text = @"申购失败";
        self.yuanPoint.backgroundColor = [UIColor colorWithHexString:stockRed];
        [self subViewLoadData:YES isFail:YES];
        [self subViewLoadData:NO isFail:YES];
        
    }else
    {
        self.rights.text = model.rightTime.length ? @"自主行权" : @"到期行权";
        self.yuanPoint.backgroundColor = NumberColor(93, 137, 246);
        [self subViewLoadData:YES isFail:NO];
        [self subViewLoadData:NO isFail:NO];
    }
    
    
    
    
   
}

+ (instancetype)cellWithTbaleview:(UITableView *)tableview
{
    static NSString *const outOptionsCell = @"YDOutOptionsCell";
    
    YDOutOptionsCell *cell = [tableview dequeueReusableCellWithIdentifier:outOptionsCell];
    if (cell == nil) {
        cell = [[YDOutOptionsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:outOptionsCell];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.leftGap = rate375(20);
        [self topview];
        [self stockName];
        [self stockCode];
        
        [self jiantou];
        [self rights];
        [self yuanPoint];
        
        [self line1];
        [self middleView];
        [self line2];
        [self bottomView];
        
        [self addChildLabelWithCount:3 SuperView:self.middleView isMiddle:YES];
        [self addChildLabelWithCount:7 SuperView:self.bottomView isMiddle:NO];
       
        
    }
    return self;
}

-(UIView *)topview
{
    if (!_topview) {
        _topview = [UIView new];
        _topview.backgroundColor = [UIColor colorWithHexString:mainGray];
        [self.contentView addSubview:_topview];
        [_topview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.contentView);
            make.height.equalTo(isPhoneX? @(rateX812(10)):@(rate750(10)));
        }];
        
    }
    return _topview;
}

-(UILabel *)stockName
{
    if (!_stockName) {
        _stockName = [UILabel new];
        _stockName.font = [UIFont systemFontOfSize:font18ScreenWRate *LKScreenW];
        _stockName.text = @"巴士在线";
        [self.contentView addSubview:_stockName];
        [_stockName  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(_leftGap);
            make.top.equalTo(_topview.mas_bottom).offset(isPhoneX? rateX812(15) : rate750(15));
            
        }];
    }
    return _stockName;
}
- (UILabel *)stockCode
{
    if (!_stockCode) {
        _stockCode = [UILabel new];
        _stockCode.font = [UIFont systemFontOfSize:font12ScreenWRate *LKScreenW];
        _stockCode.text = @"002188";
        _stockCode.textColor = [UIColor colorWithHexString:blueyGrey];
        [self.contentView addSubview:_stockCode];
        [_stockCode mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_stockName.mas_centerY);
            make.left.equalTo(_stockName.mas_right).offset(rate375(4));
        }];
        
    }
    return _stockCode;
}

-(UIImageView *)jiantou
{
    if (!_jiantou) {
        _jiantou = [UIImageView new];
        [self.contentView addSubview:_jiantou];
        [_jiantou mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_stockName.mas_centerY);
            make.right.equalTo(self.contentView).offset(-_leftGap);
        }];
    }
    return _jiantou;
}
- (UILabel *)rights
{
    if (!_rights) {
        _rights = [UILabel new];
        _rights.font = [UIFont systemFontOfSize:font12ScreenWRate *LKScreenW];
        _rights.text = @"自主行权";
        _rights.textColor = [UIColor colorWithHexString:blueyGrey];
        [self.contentView addSubview:_rights];
        [_rights mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_stockName.mas_centerY);
            make.right.equalTo(_jiantou.mas_left).offset(-6);
        }];
        
    }
    return _rights;
}
- (UIView *)yuanPoint
{
    if (!_yuanPoint) {
        _yuanPoint = [UIView new];
        _yuanPoint.backgroundColor = NumberColor(93, 137, 246);

        [self.contentView addSubview:_yuanPoint];
        [_yuanPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@8);
            make.centerY.equalTo(_stockName.mas_centerY);
            make.right.equalTo(_rights.mas_left).offset(rate375(-6));
        }];
        _yuanPoint.layer.cornerRadius = 4;
        _yuanPoint.layer.masksToBounds = YES;
    }
    return _yuanPoint;
}

-(UILabel *)line1
{
    if (!_line1) {
        _line1 = [UILabel new];
        [self.contentView addSubview:_line1];
        [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(15));
            make.right.equalTo(self.contentView).offset(-rate375(15));
            make.height.equalTo(@2);
            make.top.equalTo(_stockName.mas_bottom).offset(isPhoneX?rateX812(10) : rate750(10));
        }];
        CAShapeLayer *lineLayer = [CAShapeLayer layer];
        lineLayer.lineWidth = 1.0f;
        lineLayer.strokeColor = NumberColor(220, 222, 227).CGColor;
        [lineLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:4],[NSNumber numberWithInt:3],nil]];
        [_line1.layer addSublayer:lineLayer];
  
        
        //绘制虚线
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(LKScreenW - 2 * rate375(15), 0)];
        lineLayer.path = path.CGPath;
    }
    return _line1;
}

-(UIView *)middleView
{
    if (!_middleView) {
        _middleView = [UIView new];
        
        [self.contentView addSubview:_middleView];
        [_middleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(_line1.mas_bottom);
            make.height.equalTo(isPhoneX? @(rateX812(100)) : @(rate750(100)));
        }];
    }
    return _middleView;
}

-(UILabel *)line2
{
    if (!_line2) {
        _line2 = [UILabel new];
        [self.contentView addSubview:_line2];
        [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(15));
            make.right.equalTo(self.contentView).offset(-rate375(15));
            make.height.equalTo(@1);
            make.top.equalTo(_middleView.mas_bottom);
        }];
        CAShapeLayer *lineLayer = [CAShapeLayer layer];
        lineLayer.lineWidth = 1.0f;
        lineLayer.strokeColor = NumberColor(220, 222, 227).CGColor;
        [lineLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:4],[NSNumber numberWithInt:2],nil]];
        [_line2.layer addSublayer:lineLayer];
        
        
        //绘制虚线
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(LKScreenW - 2 * rate375(15), 0)];
        lineLayer.path = path.CGPath;
        
    }
    return _line2;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        
        [self.contentView addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(_line2.mas_bottom);
            make.height.equalTo(isPhoneX? @(rateX812(230)) : @(rate750(230)));
        }];
    }
    return _bottomView;
}


- (void)addChildLabelWithCount:(NSInteger)count SuperView:(UIView *)view isMiddle:(BOOL)isMiddle
{
    
    CGFloat x = rate375(20);
    CGFloat y = isPhoneX ? rateX812(10) : rate750(10);
    CGFloat w = rate375(80);
    CGFloat h = isPhoneX ? rateX812(20) : rate750(20);
    
    for (int i = 0; i < count; i++) {
        UILabel *leftLbel = [UILabel new];
        leftLbel.tag = isMiddle ? 11+i : 22+i;
        leftLbel.textColor = [UIColor colorWithHexString:blueyGrey];
        leftLbel.frame = CGRectMake(x, y + i * (h + y), w, h);
        leftLbel.font= [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        
//        leftLbel.text = @"名义本金";
        
        [view addSubview:leftLbel];
        
        
        UILabel *rightLbl = [UILabel new];
        rightLbl.textAlignment = NSTextAlignmentRight;
        rightLbl.tag = isMiddle ? 77+i : 88+i;
        rightLbl.textColor = NumberColor(30, 36, 46);
        rightLbl.frame = CGRectMake(leftLbel.right, y + i * (h + y), LKScreenW - leftLbel.right - 20, h);
        rightLbl.font= [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
//        rightLbl.text = @"16400元";
        
        [view addSubview:rightLbl];
       

        
    }
    
    
}
- (void)subViewLoadData:(BOOL)isMiddle isFail:(BOOL)isFail
{
    if (isMiddle) {
        for (int i = 0; i <3 ; i++) {
            UILabel *leftLbel = (UILabel *)[self.contentView viewWithTag:i + 11];
            UILabel *rightLbl = (UILabel *)[self.contentView viewWithTag:i + 77];
            
            switch (i) {
                case 0:
                    leftLbel.text = @"名义本金";
                    rightLbl.text = self.model.wan;
                    break;
                case 1:{
                    leftLbel.text = @"结算盈亏";
                    
                    if (self.model.profit.floatValue > 0) {
                        rightLbl.textColor = [UIColor colorWithHexString:stockRed];
                        rightLbl.text = [NSString stringWithFormat:@"+%@元",[self.model.profit pz_stringNumberByMultiplyingBy:@"1"]];
                    }else
                    {
                        rightLbl.textColor = [UIColor colorWithHexString:stockGreen];
                        rightLbl.text = self.model.profit;
                    }
                    
                }
                    break;
                case 2:{
                        //申购失败
                        leftLbel.text = isFail ?  @"退还权利金" : @"保证金";
                        rightLbl.text = [NSString stringWithFormat:@"%@元",[self.model.rightMoney pz_stringNumberByMultiplyingBy:@"1"]];
                }
                default:
                    break;
            }
        }
    }else
    {
        for (int i = 0; i <7 ; i++) {
            UILabel *leftLbel = (UILabel *)[self.contentView viewWithTag:i + 22];
            UILabel *rightLbl = (UILabel *)[self.contentView viewWithTag:i + 88];
            
            switch (i) {
                case 0:
                    leftLbel.text = @"标的股票";
                    rightLbl.text = self.model.biaoStock;
                    break;
                case 1:
                    leftLbel.text = @"买入价";
                 
                    rightLbl.text =  [NSString stringWithFormat:@"%.2f",_model.buyingPrice.floatValue];
                    break;
                case 2:
                    leftLbel.text = @"卖出价";
                    rightLbl.text = [NSString stringWithFormat:@"%.2f",_model.sellingPrice.floatValue];
                    break;
                case 3:
                    leftLbel.text = @"买入时间";
                    rightLbl.text = self.model.buyingTime.length ? self.model.buyingTime : @"--";
                    break;
                case 4:
                    leftLbel.text = @"卖出时间";
                    rightLbl.text = self.model.sellingTime.length ? self.model.sellingTime : @"--";
                    break;
                case 5:
                    leftLbel.text = @"行权周期";
                    rightLbl.text = self.model.cycleName;
                    break;
                case 6:
                    leftLbel.text = @"订单编号";
                    rightLbl.text = self.model.tradeNo;
                    break;
                default:
                    break;
            }
        }
        
    }
    
}
@end
